package blog.spring.db.service.impl;

import java.util.List;
import java.util.Map;

import blog.core.exception.FormParamErrorException;
import blog.core.exception.NotFoundException;
import blog.core.util.ExceptionUtils;
import blog.spring.db.dao.IBaseDAO;
import blog.spring.db.service.IBaseService;

public class BaseService<T> implements IBaseService<T> {

	private IBaseDAO<T> dao;
	private final String TYPE_NAME;

	public BaseService(IBaseDAO<T> dao, String TYPE_NAME) {
		this.dao = dao;
		this.TYPE_NAME = TYPE_NAME;
	}

	@Override
	public void save(T entity) throws FormParamErrorException {
		dao.insert(entity);
	}

	@Override
	public void remove(T entity) {
		dao.delete(entity);
	}

	@Override
	public void update(T entity) throws FormParamErrorException,
			NotFoundException {
		dao.update(entity);
	}

	@Override
	public T findById(int id) throws NotFoundException {
		T t = dao.selectById(id);
		if (t == null)
			ExceptionUtils.NewNotFound(id, TYPE_NAME);
		return t;
	}

	@Override
	public T findByName(String name) {
		return dao.selectByName(name);
	}

	@Override
	public boolean findByNameIsExist(String name) {
		return dao.selectByNameIsExist(name);
	}

	@Override
	public List<T> findList(Integer pageNum, Integer pageSize, String name,
			String sort) {
		return dao.selectList(pageNum, pageSize, name, sort);
	}

	@Override
	public List<T> searchList(Map<String, Object> conditions, Integer pageNum,
			Integer pageSize, String name, String sort, Boolean islike) {
		return dao
				.searchList(conditions, pageNum, pageSize, name, sort, islike);
	}

	@Override
	public List<T> findAll(){
		return dao.selectAll();
	}

	@Override
	public T searchUnique(Map<String, Object> conditions, Boolean islike) {
		return dao.searchUnique(conditions, islike);
	}



}
